package com.baidu.parse.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.baidu.parse.service.ParseService;
import com.baidu.user.bean.App;
import com.baidu.user.bean.AppType;
import com.baidu.user.bean.Pic;
import com.baidu.util.FreemarkerUtil;
import com.baidu.util.ParseUtil;

public class ParseThread extends Thread{
	
	
	private int startPage;
	
	private int endPage;
	
	private ParseService service;
	

	public ParseThread(ParseService service, int startPage, int endPage) {
		super();
		this.startPage = startPage;
		this.endPage = endPage;
		this.service = service;
	}


	public void run(){
		
		synchronized (ParseThread.class) {
			
			try {
				
				List appList = new ArrayList();
				
				List<AppType> typeList = service.getAppType();
						
				List<AppType> appTypeList = new ArrayList<AppType>();
				
				for (int i = startPage; i < endPage; i++) {
					
					Parser parser = new Parser("http://mm.10086.cn/android/software/qbrj?p="+i);
					
					parser.setEncoding("utf-8");
					
					HasAttributeFilter attributeFilter = new HasAttributeFilter("class", "autoword");
					
					NodeList list = parser.parse(attributeFilter);
					
					
					
					for (int j = 0; j < list.size(); j++) {
						
						String nodeUrl = ((TagNode)list.elementAt(j)).getAttribute("href");
						
						Map map2 = ParseUtil.parsea(nodeUrl);
						
						App app =  (App) map2.get("app");
						
						List<Pic> piclist = (List<Pic>) map2.get("piclist");
						
						Map map = new HashMap();
						
						map.put("app", app);
						
						map.put("pic1", piclist.get(0).getPicurl());
						
						map.put("pic2", piclist.get(1).getPicurl());
						
						map.put("pic3", piclist.get(2).getPicurl());
						
						FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
						
						freemarkerUtil.fprint("detailed.ftl", map, "E:\\apphtml/"+app.getId()+".html");
						
						String apptype = app.getApptype();
						
						String[] atype = apptype.split(" ");
						
						
						if(typeList.size() == 0){
							
							AppType type1 = new AppType();
							
							type1.setId(UUID.randomUUID().toString());
							
							type1.setTypeName(atype[0]);
							
							System.out.println(atype[0]);
							
							type1.setPid("0");
							
							AppType type2 = new AppType();
							
							type2.setId(UUID.randomUUID().toString());
							
							type2.setTypeName(atype[1]);
							
							System.out.println(atype[1]);
							
							type2.setPid(type1.getId());
							
							app.setApptype(type2.getId());
							
							typeList.add(type1);
							
							typeList.add(type2);
							
							appTypeList.add(type1);
							
							appTypeList.add(type2);
							
						}else{
							
							int flag = 0;
							
							String pid = null;
							
							for (AppType a : typeList) {
								
								if(a.getTypeName().equals(atype[0])){
									
									pid = a.getId();
									
									flag = 1;
									
									break;
								}
							}
							
							if(flag != 1){
								
								AppType type1 = new AppType();
								
								type1.setId(UUID.randomUUID().toString());
								
								type1.setTypeName(atype[0]);
								
								System.out.println(atype[0]);
								
								type1.setPid("0");
								
								AppType type2 = new AppType();
								
								type2.setId(UUID.randomUUID().toString());
								
								type2.setTypeName(atype[1]);
								
								System.out.println(atype[1]);
								
								type2.setPid(type1.getId());
								
								app.setApptype(type2.getId());
								
								typeList.add(type1);
								
								typeList.add(type2);
								
								appTypeList.add(type1);
								
								appTypeList.add(type2);
								
							}else{
								
								
								int flag1 = 0 ;
								
								for (AppType a : typeList) {
									
									if(a.getTypeName().equals(atype[1])){
										
										app.setApptype(a.getId());
										
										flag1 = 1;
										
										break;
									}
								}
								
								if(flag1 != 1){
									
									AppType type2 = new AppType();
									
									type2.setId(UUID.randomUUID().toString());
									
									type2.setTypeName(atype[1]);
									
									type2.setPid(pid);
									
									app.setApptype(type2.getId());
									
									typeList.add(type2);
									
									appTypeList.add(type2);
								}
							}
							
						}
						
						
						appList.add(app);
						
					}
					
					
				}
				
				service.addApp(appList);
				
				service.addAppType(appTypeList);
			
			} catch (ParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
